package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.VisitRecord;
import com.ruoyi.system.mapper.AppointmentMapper;
import com.ruoyi.system.domain.Appointment;
import com.ruoyi.system.service.IAppointmentService;

/**
 * 预约主表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-05
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService 
{
    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询预约主表
     * 
     * @param id 预约主表主键
     * @return 预约主表
     */
    @Override
    public Appointment selectAppointmentById(Integer id)
    {
        return appointmentMapper.selectAppointmentById(id);
    }

    /**
     * 查询预约主表列表
     * 
     * @param appointment 预约主表
     * @return 预约主表
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment)
    {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增预约主表
     * 
     * @param appointment 预约主表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAppointment(Appointment appointment)
    {
        appointment.setCreateTime(DateUtils.getNowDate());
        int rows = appointmentMapper.insertAppointment(appointment);
        insertVisitRecord(appointment);
        return rows;
    }

    /**
     * 修改预约主表
     * 
     * @param appointment 预约主表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAppointment(Appointment appointment)
    {
        appointmentMapper.deleteVisitRecordByAppointmentId(appointment.getId());
        insertVisitRecord(appointment);
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除预约主表
     * 
     * @param ids 需要删除的预约主表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAppointmentByIds(Integer[] ids)
    {
        appointmentMapper.deleteVisitRecordByAppointmentIds(ids);
        return appointmentMapper.deleteAppointmentByIds(ids);
    }

    /**
     * 删除预约主表信息
     * 
     * @param id 预约主表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAppointmentById(Integer id)
    {
        appointmentMapper.deleteVisitRecordByAppointmentId(id);
        return appointmentMapper.deleteAppointmentById(id);
    }

    /**
     * 新增就诊记录子信息
     * 
     * @param appointment 预约主表对象
     */
    public void insertVisitRecord(Appointment appointment)
    {
        List<VisitRecord> visitRecordList = appointment.getVisitRecordList();
        Integer id = appointment.getId();
        if (StringUtils.isNotNull(visitRecordList))
        {
            List<VisitRecord> list = new ArrayList<VisitRecord>();
            for (VisitRecord visitRecord : visitRecordList)
            {
                visitRecord.setAppointmentId(id);
                list.add(visitRecord);
            }
            if (list.size() > 0)
            {
                appointmentMapper.batchVisitRecord(list);
            }
        }
    }

    /**
     * 通过医生姓名查找该医生所有的患者
     */
    @Override
    public int selectAllPatientByDoctorName(String doctorName) {
        return appointmentMapper.selectAllPatientByDoctorName(doctorName);
    }
}
